/**
 * jims
 */
package com.jims.master.modules.blood.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.blood.entity.BloodOutpDetail;
import com.jims.master.modules.blood.service.BloodExportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 血液出库Controller
 * @author fengyg
 * @version 2017-04-10
 */
@Controller
@RequestMapping(value = "${adminPath}/blood/bloodExport")
public class BloodExportController extends BaseController {

	@Autowired
	private BloodExportService bloodExportService;
	
	@ModelAttribute
	public BloodOutpDetail get(@RequestParam(required=false) String id) {
		BloodOutpDetail entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bloodExportService.get(id);
		}
		if (entity == null){
			entity = new BloodOutpDetail();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(BloodOutpDetail bloodOutpDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BloodOutpDetail> page = bloodExportService.findPage(new Page<BloodOutpDetail>(request, response), bloodOutpDetail);
        page.setList(null);
		model.addAttribute("page", page);
		return "modules/blood/bloodExportIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(BloodOutpDetail bloodOutpDetail, Model model) {
		model.addAttribute("bloodOutpDetail", bloodOutpDetail);
		return "modules/blood/bloodExportForm";
	}

    /**
     * 获取当前机构最大出库单号
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "getMaxOutpNo")
    @ResponseBody
    public StringData getMaxOutpNo() {
        String inpNo = bloodExportService.getMaxOutpNo();
        StringData data = new StringData();
        data.setCode("success");
        data.setData(inpNo);
        return data;
    }

    /**
     * 自动补全血液入库单号
     * @param q 传递参数
     * @param limit
     * @param bloodOutpDetail
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "autoCompleteInpNo")
    @ResponseBody
    public List<BloodOutpDetail> autoCompleteInpNo(String q,String limit,BloodOutpDetail bloodOutpDetail){
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return bloodExportService.autoCompleteInpNo(q,limit,bloodOutpDetail);
    }

    /**
     * 血液出库保存
     * @param bloodOutpDetail
     * @param model
     * @return
     * @author fengyg
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData saveBloodExport(BloodOutpDetail bloodOutpDetail, Model model) throws Exception{
        StringData data = new StringData();
        int i = bloodExportService.saveBloodExport(bloodOutpDetail);
        if (i > 0) {
            data.setCode("success");
            data.setData("保存成功");
        } else {
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}

    /**
     * 出库单查询
     * @param bloodOutpDetail
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"bloodExportSearch",""})
    public String bloodExportQuery(BloodOutpDetail bloodOutpDetail,HttpServletRequest request,HttpServletResponse response,Model model){
        List<BloodOutpDetail> list = bloodExportService.bloodExportQuery(bloodOutpDetail);
        model.addAttribute("list",list);
        return "modules/blood/bloodExportSearch";
    }

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(BloodOutpDetail bloodOutpDetail) {
	    StringData data=new StringData();
		int i= bloodExportService.delete(bloodOutpDetail);
        if(i>0){
            data.setCode("success");
            data.setData("删除血液出库明细记录成功");
        }else{
            data.setCode("error");
            data.setData("删除血液出库明细记录失败");
        }
		return data;
	}

}